package com.gqj.meter.controller;

import com.gqj.common.core.controller.BaseController;
import com.gqj.common.core.domain.AjaxResult;
import com.gqj.common.core.domain.entity.SysDictData;
import com.gqj.common.core.domain.entity.SysDictType;
import com.gqj.common.core.domain.entity.SysUser;
import com.gqj.common.core.page.TableDataInfo;
import com.gqj.common.utils.StringUtils;
import com.gqj.meter.domain.*;
import com.gqj.meter.service.*;
import com.gqj.meter.service.impl.MdInTaskDetailsServiceImpl;
import com.gqj.meter.service.impl.MdOutTaskDetailsServiceImpl;
import com.gqj.soapclient.client;
import com.gqj.system.service.ISysDictDataService;
import com.gqj.system.service.ISysDictTypeService;
import com.gqj.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.List;

/**
 * 硬件接口Controller
 *
 * @author lxq
 * @date lxq-03-03
 */
@Api(value = "/meter/interface", description = "硬件接口")
@RestController
@RequestMapping("/meter/interface")
public class MdInterfaceController extends BaseController {
    @Autowired
    private MdOutTaskDetailsServiceImpl mdOutTaskDetailsServiceImp;
    @Autowired
    private IMdOutTaskService mdOutTaskService;
    @Autowired
    private IMdInTaskService mdInTaskService;
    @Autowired
    private MdInTaskDetailsServiceImpl mdInTaskDetailsService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IMdOutTaskDetailsService mdOutTaskDetailsService;
    @Autowired
    private ISysDictTypeService dictTypeService;
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private IMdMeterService mdMeterService;
    @Autowired
    private IMdItService mdItService;
    @Autowired
    private IMdLcEquipService mdLcEquipService;
    @Autowired
    private IMdComModuleService mdComModuleService;
    @Autowired
    private IMdConcentratorService mdConcentratorService;
    @Autowired
    private IMdOtherMaterialService mdOtherMaterialService;
    @Autowired
    private IMdStockPosInfoService mdStockPosInfoService;
    @Autowired
    private IMdChkTaskService mdChkTaskService;
    @Autowired
    private IMdChkTaskDetailsService mdChkTaskDetailsService;
    @Autowired
    private IMdStoreLocChgService mdStoreLocChgService;

    /**
     * 将出库设备结果信息插入到中间数据库
     */
    @ApiOperation("将出库设备结果信息插入到中间数据库")
    @PostMapping("/insertMIoDetInfoOut")
    public AjaxResult insertMIoDetInfoOut(@RequestBody MdOutTaskDetailsController.MdOutTaskMap mdOutTaskMap) throws IOException, ParserConfigurationException, SAXException {
        Long outTaskId = mdOutTaskMap.getOutTaskId();
        if (null == outTaskId || outTaskId == 0) {
            return AjaxResult.error("任务ID为空，请联系系统管理员！");
        }

        String outTaskNo = mdOutTaskMap.getOutTaskNo();
        if (StringUtils.isNull(outTaskNo)) {
            return AjaxResult.error("任务编号为空，请联系系统管理员！");
        }

        String assetsCategory = mdOutTaskMap.getAssetsCategory();
        if (StringUtils.isNull(assetsCategory)) {
            return AjaxResult.error("资产大类为空，请联系系统管理员！");
        }

        MdOutTask mdOutTask = mdOutTaskService.selectMdOutTaskByOutTaskId(outTaskId);
        if (null == mdOutTask) {
            return AjaxResult.error("任务不存在，请联系系统管理员！");
        }
        if (!outTaskNo.equals(mdOutTask.getOutTaskNo())) {
            return AjaxResult.error("任务编号不匹配，请联系系统管理员！");
        }
        if (!assetsCategory.equals(mdOutTask.getAssetsCategory())) {
            return AjaxResult.error("资产大类不匹配，请联系系统管理员！");
        }
        List<MdOutTaskDetails> mdOutTaskDetailsList = mdOutTaskMap.getMdOutTaskDetailsList();
        if (null == mdOutTaskDetailsList || mdOutTaskDetailsList.size() == 0) {
            return AjaxResult.error("任务明细为空，请联系系统管理员！");
        }
        mdOutTaskMap.setTaskOrderType(mdOutTask.getTaskOrderType());
        int resultNum = 0;
        if ("2".equals(mdOutTaskMap.getTaskOrderType())) {
            resultNum =  mdOutTaskDetailsServiceImp.insetMdIoDetInfo(mdOutTaskMap);
        }
        if (resultNum > 0) {
            client.sibSendIOInfo(mdOutTask.getOperId().toString());
        }
        return toAjax(1);
    }

    /**
     * 将入库设备结果信息插入到中间数据库
     */
    @ApiOperation("将入库设备结果信息插入到中间数据库")
    @PostMapping("/insertMIoDetInfoIn")
    public AjaxResult insertMIoDetInfoIn(@RequestBody MdInTaskDetailsController.MdInTaskMap mdInTaskMap) throws IOException, ParserConfigurationException, SAXException {
        Long inTaskId = mdInTaskMap.getInTaskId();
        if (null == inTaskId || inTaskId == 0) {
            return AjaxResult.error("任务ID为空，请联系系统管理员！");
        }

        String inTaskNo = mdInTaskMap.getInTaskNo();
        if (null == inTaskNo || "".equals(inTaskNo)) {
            return AjaxResult.error("任务编号为空，请联系系统管理员！");
        }

        String assetsCategory = mdInTaskMap.getAssetsCategory();
        if (null == assetsCategory || "".equals(assetsCategory)) {
            return AjaxResult.error("资产大类为空，请联系系统管理员！");
        }

        MdInTask mdInTask = mdInTaskService.selectMdInTaskByInTaskId(inTaskId);
        if (null == mdInTask) {
            return AjaxResult.error("任务不存在，请联系系统管理员！");
        }
        if (!inTaskNo.equals(mdInTask.getInTaskNo())) {
            return AjaxResult.error("任务编号不匹配，请联系系统管理员！");
        }
        if (!assetsCategory.equals(mdInTask.getAssetsCategory())) {
            return AjaxResult.error("资产大类不匹配，请联系系统管理员！");
        }

        List<MdInTaskDetails> mdInTaskDetailsList = mdInTaskMap.getMdInTaskDetailsList();
        if (null == mdInTaskDetailsList || mdInTaskDetailsList.size() == 0) {
            return AjaxResult.error("任务明细为空，请联系系统管理员！");
        }
        mdInTaskMap.setTaskOrderType(mdInTask.getTaskOrderType());
        int resultNum = 0;
        if ("2".equals(mdInTaskMap.getTaskOrderType())) {
            resultNum = mdInTaskDetailsService.insetMdIoDetInfo(mdInTaskMap);
        }
        if (resultNum > 0) {
            client.sibSendIOInfo(mdInTask.getOperId().toString());
        }



        return toAjax(1);
    }


    /**
     * 出入库任务的状态上传给计量服务总线接口
     */
    @ApiOperation("出入库任务的状态上传给计量服务总线接口")
    @GetMapping("/updateOrderStatus")
    public AjaxResult updateOrderStatus(String ioTaskNo, String taskStatus) throws IOException, ParserConfigurationException, SAXException {

        System.out.println(ioTaskNo + "状态" + taskStatus);
        //调用WebService接口通知计量系统出库完成
        client.sibUpIOResult(ioTaskNo, taskStatus);

        return toAjax(1);
    }

    /**
     * 查询员工工号
     */
    @ApiOperation("查询员工工号")
    @GetMapping("selectUserByUserName")
    public AjaxResult selectUserByUserName(String userName) {
        boolean isLogin = false;
        if (null == userName || "".equals(userName)) {
            return AjaxResult.error("员工工号为空，请检查重试！");
        }
        SysUser sysUser = sysUserService.selectUserByUserName(userName);
        if (null != sysUser) {
            isLogin = true;
        }
        return success(isLogin);
    }

    /**
     * 查询入库任务列表
     */
    @ApiOperation("查询入库任务列表")
    @GetMapping("/selectInTaskList")
    public TableDataInfo selectInTaskList(MdInTask mdInTask) {
        startPage();
        List<MdInTask> list = mdInTaskService.selectMdInTaskList(mdInTask);
        return getDataTable(list);
    }

    /**
     * 查询入库明细列表
     */
    @ApiOperation("查询入库明细列表")
    @GetMapping("/selectInTaskDetailsList")
    public TableDataInfo selectInTaskDetailsList(MdInTaskDetails mdInTaskDetails) {
        startPage();
        List<MdInTaskDetails> list = mdInTaskDetailsService.selectMdInTaskDetailsList(mdInTaskDetails);
        return getDataTable(list);
    }

    /**
     * 查询出库任务列表
     */
    @ApiOperation("查询出库任务列表")
    @GetMapping("/selectOutTaskList")
    public TableDataInfo selectOutTaskList(MdOutTask mdOutTask) {
        startPage();
        List<MdOutTask> list = mdOutTaskService.selectMdOutTaskList(mdOutTask);
        return getDataTable(list);
    }

    /**
     * 查询出库明细列表
     */
    @ApiOperation("查询出库明细列表")
    @GetMapping("/selectOutTaskDetailsList")
    public TableDataInfo selectOutTaskDetailsList(MdOutTaskDetails mdOutTaskDetails) {
        startPage();
        List<MdOutTaskDetails> list = mdOutTaskDetailsService.selectMdOutTaskDetailsList(mdOutTaskDetails);
        return getDataTable(list);
    }

    /**
     * 查询字典类型
     */
    @ApiOperation("查询字典类型")
    @GetMapping("/selectDictTypeList")
    public TableDataInfo selectDictTypeList(SysDictType dictType) {
        startPage();
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        return getDataTable(list);
    }

    /**
     * 查询字典类型数据
     */
    @ApiOperation("查询字典类型数据")
    @GetMapping("/selectDictDataList")
    public TableDataInfo selectDictDataList(SysDictData dictData) {
        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }

    /**
     * 查询电能表列表
     */
    @ApiOperation("查询电能表列表")
    @GetMapping("/selectMdMeterList")
    public TableDataInfo selectMdMeterList(MdMeter mdMeter) {
        startPage();
        List<MdMeter> list = mdMeterService.selectMdMeterList(mdMeter);
        return getDataTable(list);
    }

    /**
     * 查询互感器列表
     */
    @ApiOperation("查询互感器列表")
    @GetMapping("/selectMdItList")
    public TableDataInfo selectMdItList(MdIt mdIt) {
        startPage();
        List<MdIt> list = mdItService.selectMdItList(mdIt);
        return getDataTable(list);
    }

    /**
     * 查询终端列表
     */
    @ApiOperation("查询终端列表")
    @GetMapping("/selectMdLcEquipList")
    public TableDataInfo selectMdLcEquipList(MdLcEquip mdLcEquip) {
        startPage();
        List<MdLcEquip> list = mdLcEquipService.selectMdLcEquipList(mdLcEquip);
        return getDataTable(list);
    }

    /**
     * 查询模块列表
     */
    @ApiOperation("查询模块列表")
    @GetMapping("/selectMdComModuleList")
    public TableDataInfo selectMdComModuleList(MdComModule mdComModule) {
        startPage();
        List<MdComModule> list = mdComModuleService.selectMdComModuleList(mdComModule);
        return getDataTable(list);
    }

    /**
     * 查询集中器列表
     */
    @ApiOperation("查询集中器列表")
    @GetMapping("/selectMdConcentratorList")
    public TableDataInfo selectMdConcentratorList(MdConcentrator mdConcentrator) {
        startPage();
        List<MdConcentrator> list = mdConcentratorService.selectMdConcentratorList(mdConcentrator);
        return getDataTable(list);
    }

    /**
     * 查询其他物资列表
     */
    @ApiOperation("查询其他物资列表")
    @GetMapping("/selectMdOtherMaterialList")
    public TableDataInfo selectMdOtherMaterialList(MdOtherMaterial mdOtherMaterial) {
        startPage();
        List<MdOtherMaterial> list = mdOtherMaterialService.selectMdOtherMaterialList(mdOtherMaterial);
        return getDataTable(list);
    }

    /**
     * 查询储位信息列表
     */
    @ApiOperation("查询储位信息列表")
    @GetMapping("/selectMdStockPosInfoList")
    public TableDataInfo selectMdStockPosInfoList(MdStockPosInfo mdStockPosInfo) {
        startPage();
        List<MdStockPosInfo> list = mdStockPosInfoService.selectMdStockPosInfoList(mdStockPosInfo);
        return getDataTable(list);
    }

    /**
     * 查询盘点任务列表
     */
    @ApiOperation("查询盘点任务列表")
    @GetMapping("/selectMdChkTaskList")
    public TableDataInfo selectMdChkTaskList(MdChkTask mdChkTask) {
        startPage();
        List<MdChkTask> list = mdChkTaskService.selectMdChkTaskList(mdChkTask);
        return getDataTable(list);
    }

    /**
     * 查询盘点任务详情列表
     */
    @ApiOperation("查询盘点任务详情列表")
    @GetMapping("/selectMdChkTaskDetailsList")
    public TableDataInfo selectMdChkTaskDetailsList(MdChkTaskDetails mdChkTaskDetails) {
        startPage();
        List<MdChkTaskDetails> list = mdChkTaskDetailsService.selectMdChkTaskDetailsList(mdChkTaskDetails);
        return getDataTable(list);
    }
    /*
     * 入库结果上传
     * */
    @ApiOperation("入库结果上传")
    @PostMapping("/handsetInResultComplete")
    public AjaxResult handsetInResultComplete(@RequestBody MdInTaskDetailsController.MdInTaskMap mdInTaskMap) throws IOException, ParserConfigurationException, SAXException {
        Long inTaskId = mdInTaskMap.getInTaskId();
        if (null == inTaskId || inTaskId == 0) {
            return AjaxResult.error("任务ID为空，请联系系统管理员！");
        }

        String inTaskNo = mdInTaskMap.getInTaskNo();
        if (null == inTaskNo || "".equals(inTaskNo)) {
            return AjaxResult.error("任务编号为空，请联系系统管理员！");
        }

        String assetsCategory = mdInTaskMap.getAssetsCategory();
        if (null == assetsCategory || "".equals(assetsCategory)) {
            return AjaxResult.error("资产大类为空，请联系系统管理员！");
        }

        MdInTask mdInTask = mdInTaskService.selectMdInTaskByInTaskId(inTaskId);
        if (null == mdInTask) {
            return AjaxResult.error("任务不存在，请联系系统管理员！");
        }
        if (!inTaskNo.equals(mdInTask.getInTaskNo())) {
            return AjaxResult.error("任务编号不匹配，请联系系统管理员！");
        }
        if (!assetsCategory.equals(mdInTask.getAssetsCategory())) {
            return AjaxResult.error("资产大类不匹配，请联系系统管理员！");
        }
        if (!"1".equals(mdInTask.getStatusCode())) {
            return AjaxResult.error("任务不是执行中状态，无法修改明细！");
        }

        List<MdInTaskDetails> mdInTaskDetailsList = mdInTaskMap.getMdInTaskDetailsList();
        if (null == mdInTaskDetailsList || mdInTaskDetailsList.size() == 0) {
            return AjaxResult.error("任务明细为空，请联系系统管理员！");
        }
        mdInTaskMap.setTaskOrderType(mdInTask.getTaskOrderType());
        mdInTaskMap.setOperId(mdInTask.getOperId());
        mdInTaskMap.setUseEquipment("02");
        return toAjax(mdInTaskDetailsService.updateMdInTaskDetails(mdInTaskMap));
    }

    /*
     * 出库结果上传
     * */
    @ApiOperation("出库结果上传")
    @PostMapping("/handsetOutResultComplete")
    public AjaxResult handsetOutResultComplete(@RequestBody MdOutTaskDetailsController.MdOutTaskMap mdOutTaskMap) throws Exception {

        Long outTaskId = mdOutTaskMap.getOutTaskId();
        if (null == outTaskId || outTaskId == 0) {
            return AjaxResult.error("任务ID为空，请联系系统管理员！");
        }

        String outTaskNo = mdOutTaskMap.getOutTaskNo();
        if (StringUtils.isNull(outTaskNo)) {
            return AjaxResult.error("任务编号为空，请联系系统管理员！");
        }

        String assetsCategory = mdOutTaskMap.getAssetsCategory();
        if (StringUtils.isNull(assetsCategory)) {
            return AjaxResult.error("资产大类为空，请联系系统管理员！");
        }

        MdOutTask mdOutTask = mdOutTaskService.selectMdOutTaskByOutTaskId(outTaskId);
        if (null == mdOutTask) {
            return AjaxResult.error("任务不存在，请联系系统管理员！");
        }
        if (!outTaskNo.equals(mdOutTask.getOutTaskNo())) {
            return AjaxResult.error("任务编号不匹配，请联系系统管理员！");
        }
        if (!assetsCategory.equals(mdOutTask.getAssetsCategory())) {
            return AjaxResult.error("资产大类不匹配，请联系系统管理员！");
        }
        if (!"1".equals(mdOutTask.getStatusCode())) {
            return AjaxResult.error("任务不是执行中状态，无法修改明细！");
        }

        List<MdOutTaskDetails> mdOutTaskDetailsList = mdOutTaskMap.getMdOutTaskDetailsList();
        if (null == mdOutTaskDetailsList || mdOutTaskDetailsList.size() == 0) {
            return AjaxResult.error("任务明细为空，请联系系统管理员！");
        }
        mdOutTaskMap.setTaskOrderType(mdOutTask.getTaskOrderType());
        mdOutTaskMap.setOperId(mdOutTask.getOperId());
        mdOutTaskMap.setUseEquipment("02");
        return toAjax(mdOutTaskDetailsService.updateMdOutTaskDetails(mdOutTaskMap));
    }

    /*
     * 盘点结果上传
     * */
    @ApiOperation("盘点结果上传")
    @PostMapping("/handsetCheckResultComplete")
    public AjaxResult handsetCheckResultComplete(@RequestBody MdChkTask mdChkTask) {
        Long chkTaskId = mdChkTask.getChkTaskId();
        if (null == chkTaskId || chkTaskId == 0) {
            return AjaxResult.error("任务ID为空，请联系系统管理员！");
        }

        String assetsCategory = mdChkTask.getAssetsCategory();
        if (StringUtils.isNull(assetsCategory)) {
            return AjaxResult.error("资产大类为空，请联系系统管理员！");
        }

        MdChkTask chkTask = mdChkTaskService.selectMdChkTaskByChkTaskId(chkTaskId);
        if (null == chkTask) {
            return AjaxResult.error("任务不存在，请联系系统管理员！");
        }
        if (!assetsCategory.equals(chkTask.getAssetsCategory())) {
            return AjaxResult.error("资产大类不匹配，请联系系统管理员！");
        }
        if (!"02".equals(chkTask.getStatusCode())) {
            return AjaxResult.error("任务不是执行中状态，无法修改明细！");
        }
        mdChkTask.setUseEquipment("02");

        return toAjax(mdChkTaskService.checkMdChkTask(mdChkTask));
    }

    /*
     * 上传储位变更结果
     * */
    @ApiOperation("盘点结果上传")
    @PostMapping("/handsetStoreLocChgRuseltComplete")
    public AjaxResult handsetStoreLocChgResultComplete(@RequestBody MdStoreLocChg mdStoreLocChg) {
        //储位变更
        String equipCateg = mdStoreLocChg.getEquipCateg();
        if (StringUtils.isNull(equipCateg)) {
            return AjaxResult.error("资产大类为空，请联系系统管理员！");
        }
        List<String> rfidList = mdStoreLocChg.getRfidList();
        if (null == rfidList || rfidList.size() == 0) {
            return AjaxResult.error("任务明细为空，请联系系统管理员！");
        }

        return toAjax(mdStoreLocChgService.insertMdStoreLocChg(mdStoreLocChg));
    }


}
